package cvo.chess.client.entities.game;

import java.util.*;

import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.Tile;
/**
 * 
 * @author Stijn.Van.Antwerpen
 *
 * Represents a Chesboard, this is a composit off Tiles
 * Pieces are placed on a chessboard, but not part of the chessboard.
 * Therefore a chessboard does not have any knowledge 
 */
public class Chessboard implements IChessboard {
	
	//Tiles contains all tiles on a chesboard
	private List<Tile> tiles = new ArrayList<Tile>();
	private int width;
	private int height;
	
	/**
	 * 
	 * @param n 
	 */
	public Chessboard(Integer n){
		this.width = n;
		this.height = n;
		RenderTiles(n,n);
	}

	private void RenderTiles(Integer cols, Integer rows) {
		for(int col = 0; col < cols; col++ )
		{
			for(int row = 0; row < rows; row++)
			{
				//Please not, we do add the tiles in order as they appear on the board
				//starting in the left upper corner, going to the right 
				//and then down.
				//we relay on this in getTileAt method
				tiles.add(new Tile(new ChessboardCoordinate(this, row, col)));
			}
		}
	}

	/* (non-Javadoc)
	 * @see cvo.chess.client.entities.game.IChessboard#getWidth()
	 */
	@Override
	public int getWidth() {
		return width;
	}

	/* (non-Javadoc)
	 * @see cvo.chess.client.entities.game.IChessboard#getAllTiles()
	 */
	@Override
	public List<Tile> getAllTiles() {
		//TODO: this should a read only list
		return this.tiles;
	}

	/* (non-Javadoc)
	 * @see cvo.chess.client.entities.game.IChessboard#getHeight()
	 */
	@Override
	public int getHeight() {
		return height;
	}

	/* (non-Javadoc)
	 * @see cvo.chess.client.entities.game.IChessboard#getTileAt(cvo.chess.client.entities.chessboard.ChessboardCoordinate)
	 */
	@Override
	public Tile getTileAt(ChessboardCoordinate position) {
		//The tiles where added in the same order as their numeric position.
		//Therefore we may relay on the position in the array.
		return this.tiles.get(position.getNummericPosition());
	}
	
	 
}
